package cn.edu.neusoft.food729.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.retroiftdemo_729.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food729.Bean.FoodListBean;
import cn.edu.neusoft.food729.adapter.FoodListAdapter;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.listener.CollectListener;
import cn.edu.neusoft.food729.listener.FoodListListener;
import cn.edu.neusoft.food729.model.CollectModel;
import cn.edu.neusoft.food729.model.FoodListModel;

public class SearchResultActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private FoodListAdapter myAdapter;
    private int page = 1, shop_id, flag = 0;
    List<FoodListBean> list;
    Context context;

    private boolean flagcollect = false;//收藏标志
    private SharedPreferences sp;//简单存储
    private String sessionID = "";

    private FoodListListener listener = new FoodListListener() {
        @Override
        public void onResponse(List<FoodListBean> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            myAdapter.setList(list);
            recyclerView.setAdapter(myAdapter);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SearchResultActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void init() {
        recyclerView = findViewById(R.id.searchlist);

        layoutManager = new LinearLayoutManager(this);
        //准备数据
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        myAdapter = new FoodListAdapter(this);
        recyclerView.setAdapter(myAdapter);

        String image = getIntent().getStringExtra("image");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        context = SearchResultActivity.this;
        init();
        FoodListModel model = new FoodListModel();
//        String shop_name = getIntent().getStringExtra("shop_name");
//        setTitle(shop_name);

//        shop_id = getIntent().getIntExtra("shop_id", -1);
        String search = getIntent().getStringExtra("search");
        model.getSearchList(search, listener);

        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readSP();

    }

    private void readSP() {
        sessionID = sp.getString("session", null);//注意名值对的名
    }
}